﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Collections.ObjectModel;
using SRAGWMCLib.OO;
using MapSvrMngrLib;

namespace SRAGWMCLib
{
    /// <summary>
    /// Class that contains the methods of SetMapSetting, Connect, Generate Map, 
    /// Display of Features and Legend Entry Builder.
    /// </summary>
    /// 

    public class GWMHelper
    {
        /// <summary>
        /// Initializes the MapServer and adds the background color and the outputtype of the map
        /// (ACGM, JPG, PNG, SVG)
        /// </summary>
        /// <param name="objMapServer">Contains the MapServer Session</param>
        /// 

        public static void SetMapSettings(SRAMapServer objMapServer)
        {
            MAPSRVLib.GWMMapServerHelper objMapServerHelper = 
                (MAPSRVLib.GWMMapServerHelper)SRAMapServerBuilder.GetMap();
            objMapServerHelper.Clear();
            objMapServerHelper.OutputType = objMapServer.OutputType;
            objMapServerHelper.BackgroundColor = ColorTranslator.ToWin32(objMapServer.BackgroundColor);
        }

        /// <summary>
        /// Adds a connection to the MapServerManager
        /// </summary>
        /// <param name="database">Contains the values of the DataBase info</param>
        /// 

        public static void Connect(DataBase database)
        {
            MAPSRVLib.GWMMapServerHelper objMapServerHelper = (MAPSRVLib.GWMMapServerHelper)SRAMapServerBuilder.GetMap();
            objMapServerHelper.SetCoordinateSystem(database.CoordSystemPath);
            objMapServerHelper.Connect(database.DBType, database.DBPath, database.ConnectionArguments, database.ConnectionName, "");
        }

        /// <summary>
        /// Fills the atributtes of the Features
        /// </summary>
        /// <param name="LegendEntry">Allow the use of the properties of the LegendEntry Class</param>
        /// <returns>The Object Array for the Range</returns>

        public static object[] DisplayFeature(LegendEntry objLegendEntry)
        {

            MAPSRVLib.GWMMapServerHelper objMapServerHelper =
                                (MAPSRVLib.GWMMapServerHelper)SRAMapServerBuilder.GetMap();
            MAPSRVLib.GWMMarkerSystem objMarker;
            MAPSRVLib.GWMDisplayRule objDisplayRule;
            MAPSRVCOMPONENTSLib.GWMFeatureSymbology objSymbology;
            object objMarkers = objMapServerHelper.Markers;
            try
            {
                objMarker = (MAPSRVLib.GWMMarkerSystem)objMapServerHelper.AddMarker(
                            objLegendEntry.DataSource.DBMarkerName,
                             objLegendEntry.DataSource.DBConnectionName, objLegendEntry.DataSource.DBTableName, 
                             objLegendEntry.DataSource.TableFields[0].ToString(),
                             //(object) objLegendEntry.DataSource.TableFields);
                             
                             (object) new string[] {objLegendEntry.DataSource.TableFields[0].ToString()});
            }
            catch (Exception ex)
            {
                throw ex;
            }
            objMarker.AddElementsByPolygon(objLegendEntry.Filter.WhereClause,
                                            objLegendEntry.Filter.Points,
                                            objLegendEntry.Filter.NumPairs,
                                            objLegendEntry.Filter.SpatialOperator);

            objDisplayRule = (MAPSRVLib.GWMDisplayRule)objMarker.NewDisplayRule();
            objSymbology = (MAPSRVCOMPONENTSLib.GWMFeatureSymbology)objMapServerHelper.CreateObject("GMWebMap.GWMFeatureSymbology", null);

            objSymbology.Color = ColorTranslator.ToWin32(objLegendEntry.BorderColor);
            objSymbology.Style = objLegendEntry.SymbologyStyle;
            objSymbology.Weight = objLegendEntry.SymbologyWeight;
            objSymbology.FillColor = ColorTranslator.ToWin32(objLegendEntry.FillColor);

            objDisplayRule.DisplaySymbology = objSymbology;
            objDisplayRule.Tooltip = objLegendEntry.Tooltip;

            return (object[])objMarker.Range;
        }

        /// <summary>
        /// Generates the final map
        /// </summary>
        /// <param name="objRange">Conatins the values for the map scale and zoom factor</param>
        /// 

        public static void GenerateMap(Range objRange)
        {
            MAPSRVLib.GWMMapServerHelper objMapServerHelper =
                (MAPSRVLib.GWMMapServerHelper)SRAMapServerBuilder.GetMap();
            MAPSRVLib.CGWMRangeScale AS1 = null;

            object objColl = objMapServerHelper.AutoScales;
            int coutRangeScales = (int)(objColl.GetType().InvokeMember("Count", 
                System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty, null, objColl, new object[] { }));

            if (coutRangeScales == 0)
                AS1 = (MAPSRVLib.CGWMRangeScale)objMapServerHelper.AddRangeAutoScale("AS1");
            else
                AS1 = ((MAPSRVLib.CGWMRangeScale)objColl.GetType().InvokeMember("Item", 
                    System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty, null, objColl, new object[] { 1 }));
                        
            AS1.Multiplier = 1 * objRange.MagFact;
            AS1.MaximumWindow = objRange.Maximum;
            AS1.MinimumWindow = objRange.Minimum;

            objMapServerHelper.CreateMapByRange(
                                                (double)MapAttributes.Range[0],
                                                (double)MapAttributes.Range[1],
                                                (double)MapAttributes.Range[2],
                                                (double)MapAttributes.Range[3],
                                                MapAttributes.Width, 
                                                MapAttributes.Height,
                                                AS1.Name, 
                                                AS1);
            AS1 = null;
        }

       
    }
}